﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MOJsdk
{
    public class BinariesConverter
    {
        public static byte[] ConvertToByteArray(MoJBasicMovie m)
        {
            MovieBinaryPointers pt = new MovieBinaryPointers();
            byte[] bytes = new byte[10 + 50 + 50 + 4 + (5 * 1024) + 10];
            byte[] temp;

            temp = Encoding.UTF8.GetBytes(m.MovieCode);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.MovieCode + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.MovieTitle);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.MovieTitle + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.MovieOriginalTitle);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.MovieOriginalTitle + i] = temp[i];

            temp = Encoding.ASCII.GetBytes(m.MovieYear);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.MovieYear + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.MovieGenres);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.Genres + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.MovieCast);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.Cast + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.MovieDirectors);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.Directors + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.MovieWriters);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.Writers + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.MovieTagline);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.TagLine + i] = temp[i];

            temp = Encoding.UTF8.GetBytes(m.Imdb);
            for (int i = 0; i < temp.Length; i++)
                bytes[pt.Imdb + i] = temp[i];

            return bytes;
        }
        public static MoJBasicMovie ConvertToMoJBasicMovie(byte[] b)
        {
            //10 + 50 + 50 + 4 + (5 * 1024) + 10
            MovieBinaryPointers pt = new MovieBinaryPointers();
            MoJBasicMovie m = new MoJBasicMovie();

            m.MovieCode = BuildString(b.SubArray(pt.MovieCode, 10));
            m.MovieTitle = BuildString(b.SubArray(pt.MovieTitle, 50));
            m.MovieOriginalTitle = BuildString(b.SubArray(pt.MovieOriginalTitle, 50));
            m.MovieYear = Encoding.ASCII.GetString(b.SubArray(pt.MovieYear, 4));
            m.MovieGenres = BuildString(b.SubArray(pt.Genres, 1024));
            m.MovieCast = BuildString(b.SubArray(pt.Cast, 1024));
            m.MovieDirectors = BuildString(b.SubArray(pt.Directors, 1024));
            m.MovieWriters = BuildString(b.SubArray(pt.Writers, 1024));
            m.MovieTagline = BuildString(b.SubArray(pt.TagLine, 1024));
            m.Imdb = BuildString(b.SubArray(pt.Imdb, 10));

            return m;
        }

        public static string BuildString(byte[] b)
        {
            string data = Encoding.UTF8.GetString(b);
            data = data.Substring(0, data.IndexOf("\0"));

            return data;
        }
    }

    public struct MovieBinaryPointers
    {
        public Int32 MovieCode
        { get { return 0; } }
        public Int32 MovieTitle
        { get { return 10; } }
        public Int32 MovieOriginalTitle
        { get { return 60; } }
        public Int32 MovieYear
        { get { return 110; } }
        public Int32 Genres
        { get { return 114; } }
        public Int32 Cast
        { get { return 1138; } }
        public Int32 Directors
        { get { return 2162; } }
        public Int32 Writers
        { get { return 3186; } }
        public Int32 TagLine
        { get { return 4210; } }
        public Int32 Imdb
        { get { return 5234; } }
    }
}
